<?php
include_once('inc/init.php');
include_once('inc/functions.index.php');

// Get The ID From The URL
$id = 0;
if(isset($_GET['id'])) {
  $id = intval($_GET['id']);
}

// Verify if the form was sent.
$comment="";
$error_msg="";
if (isset($_POST['action'])) {
  $comment = stripslashes($_POST['comment']);
  $_SESSION['comment'] = $comment;

  if (strlen($comment)==0) {
    $error_msg .= "<p> '<strong>Comment</strong>' field is empty. </p>";
  }
  if (strlen($error_msg)==0) {
    // Verify if logged
    if (isset($_SESSION['ses_email'])) {
      $email = $_SESSION['ses_email'];
    } else {
      header("Location: login.php?next=".urlencode(full_url()));
      exit();
    }

    // Verify if the user already fingered.
    $sql_cmd = "SELECT count(*) AS comment_count FROM comments WHERE id_user = ".$_SESSION['ses_id']." AND id_finger = ".$id;
    $sql_query_count = mysql_query($sql_cmd) or die(mysql_error());
    $sql_result = mysql_fetch_assoc($sql_query_count);
    //print_r($sql_result);
    if ($sql_result['comment_count']>0)
      $_POST['type']=0;

    // Create the comment
    $sql_cmd = "INSERT INTO comments(`type`, `id_finger`, `id_user`, `comment`, `timestamp`) values(".$_POST['type'].", ".$id.", ".$_SESSION['ses_id'].", '".mysql_real_escape_string($comment)."', '".time()."')";
    $sql_query = mysql_query($sql_cmd) or die(mysql_error());

    // Update finger count
    if ($_POST['type'] !=0) {
      if ($_POST['type'] ==1) {
        $sql_cmd = "UPDATE fingers SET fingered = (fingered+1) WHERE id=$id";
      }else {
        $sql_cmd = "UPDATE fingers SET fingered = (fingered-1) WHERE id=$id";
      }
      $sql_query = mysql_query($sql_cmd) or die(mysql_error());
    }

    unset($_SESSION['comment']);
    header("Location: ".full_url());
    exit();
  }
}else {
  if (isset($_SESSION['comment']))
    $comment = $_SESSION['comment'];
}

// Get Finger From Database
$finger = Finger_get($id);

// Verify If Finger Exists
if(isset($finger['id'])) {
  $title = "The finger for " . $finger['for'];
} else {
  $title = "Unknown Finger";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title><?php echo $title; ?></title>
    <link rel="stylesheet" type="text/css" href="css/style.css" />
    <?php include_once('template/head.php');?>
  </head>

  <body>
    <div id="wrap">
      <!-- header -->
      <?php include_once('template/header.php'); ?>
      <!-- body -->
      <div id="body">
        <?php include_once('template/subheader.php'); ?>

        <h1><span class="finger_count"><?php echo $finger['fingered']; ?></span><?php echo $title; ?>
          <span class="author">by <?php echo $finger['name']; ?></span>
        </h1>
      
        <div class="body" style="font-size: 1.1em;">
        <div class="addthis_toolbox addthis_32x32_style addthis_default_style" style="float:right; margin-left: 15px;">
          <a class="addthis_button_facebook"></a>
          <a class="addthis_button_twitter"></a>
          <a class="addthis_button_email"></a>
          <a class="addthis_button_google"></a>
          <a class="addthis_button_compact"></a>
        </div>
        <div class="wide5x" style="float:left;">
          <?php
            // Show available comments
            $sql_cmd = "
      			select comments.*,users.name
      			FROM comments
      			INNER JOIN users
      				ON users.id = comments.id_user
      			WHERE id_finger=$id";
            $sql_query = mysql_query($sql_cmd) or die(mysql_error());
            if (mysql_num_rows($sql_query)>0) {
              while ($mycomment = mysql_fetch_assoc($sql_query)) {
                $user_action = '';
                switch ($mycomment['type']) {
                  case 0:
                    // Just comment
                    $class = "";
                    $user_action = "says:";
                    break;
                  case 1:
                    // Comment and add finger
                    $user_action = "gives one finger:";
                    $class = "finger_up";
                    break;
                  default:
                    // Comment and remove finger
                    $user_action = "removes one finger:";
                    $class = "finger_down";
                    break;
                }
                echo "<p class=\"form_item\"><strong class=\"".$class."\">".$mycomment['name']." ".$user_action."</strong><br />".nl2br($mycomment['comment'])."</p>";
              }
            }
            ?>
  
            <form name="comment" action="<?php echo full_url(); ?>" method="post">
              <p class="form_item" style="padding-bottom:5px;">
                <span class="label"> Your oppinion: </span>
                <span class="desc"> What do you think about it?
                </span>
                <input type="hidden" name="action" value="comment" />
                <input type="hidden" name="type" id="type" value="0" />
                <textarea name="comment" class="inputwb wide5x" rows="5" cols="60" style="font-size:14px;"><?php echo $comment; ?></textarea>
              </p>
              <p class="form_item" style="text-align:center;">
                <span class="desc">Please choose if you are with me and you add a finger or the contrary.</span>
                <input type="button" class="blue_btn" value="Add Finger" id="submit_btn" onclick="add_comment(1);" />
                <input type="button" class="red_btn" value="Remove Finger" id="submit_btn" onclick="add_comment(-1);" />
              </p>
            </form>
            <script type="text/javascript">
              function add_comment(type) {
                var type_input = document.getElementById('type');
                type_input.value = type;
                document.comment.submit();
              }
            </script>
          </div>
          <div class="clearer"></div>
        </div>
      </div>
      <!-- footer -->
      <?php include_once('template/footer.php'); ?>
    </div>
    <br /><br /> <!-- keep some space with the bottom -->
    <script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#username=atrandafir"></script>
  </body>
</html>
<?php mysql_close($db); ?>